<?php

namespace app\api\controller\v2;


use app\admin\model\cms\Live;
use app\admin\model\cms\News;
use app\admin\model\cms\PlazaPostModel;
use app\admin\model\cms\Video;
use app\admin\model\Investment;
use app\admin\model\InvestmentCategoryModel;
use app\admin\model\User;
use app\common\controller\Api;
use app\common\model\Attachment;
use app\admin\model\cms\Platepost;

/**
 * 分享相关接口
 */
class Share extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = '*';
    //无需鉴权的方法,但需要登录
//    protected $noNeedRight = '*';

    //内容详情
    public function show()
    {
        $request = $this->request->param();//参数
        $category_type = $request['category_type'];//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
        $id = $request['id'];//文章id

        switch ($category_type) {
            case 'plaza':
                $this->getPlazaShow($id);
                break;
            case 'plate':
                $this->getPlateShow($id);
                break;
            case 'news':
                $this->getNewsShow($id);
                break;
            case 'video':
                $this->getVideoShow($id);
                break;
            case 'live':
                $this->getLiveShow($id);
                break;
            case 'investment':
                $this->getInvestmentShow($id);
                break;
            default:
                $this->error('类型不正确');
        }
    }

    /**
     * 获取广场帖子详情
     * @param $user 用户信息
     * @param $id 文章/内容主健id
     */
    private function getPlazaShow($id)
    {
        $model = PlazaPostModel::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }


        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }
        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $reprint_data = [];//转发的原文信息
        if ($model['reprint_id']) {
            $reprint_article = Platepost::find($model['reprint_id']);//被转发文原文

            $r_file_img = [];
            foreach (explode(',', $reprint_article['file_img']) as $path) {
                $r_file_img[] = obsurl($path);
            }
            $r_file_video = [];
            foreach (explode(',', $reprint_article['file_video']) as $path) {
                $r_file_video[] = obsurl($path);
            }
            $r_file_audio = [];
            foreach (explode(',', $reprint_article['file_audio']) as $path) {
                $r_file_audio[] = obsurl($path);
            }
            $r_file_data = [];
            if ($reprint_article['file_data']) {
                $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                foreach ($r_attachment as $r_av) {
                    $r_file_data[] = [
                        'filename' => '复盘-' . $r_av['filename'],
                        'url' => obsurl($r_av['url'])
                    ];
                }
            }

            $reprint_data = [
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $reprint_article['id'],
                'post_title' => $reprint_article['postname'],//标题
                'post_content' => $reprint_article['postcontent'],//内容
                'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                'file_img' => array_filter($r_file_img),//图片数组
                'file_img_num' => count(array_filter($r_file_img)),//图片数组
                'file_video' => array_filter($r_file_video),//视频数组
                'file_audio' => array_filter($r_file_audio),//音频数组
                'file_data' => array_filter($r_file_data),//文件数组
            ];
        }

        $content_info = [
            'id' => $model['id'],
            'title' => $model['postname'],//帖子标题
            'content' => $model['postcontent'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['plaza_category_id'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => $model['createtime'],//创建时间
            'is_like' => 0,//是否点赞|0:否
            'is_favorite' => 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'reprint_id' => $model['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
            'reprint_data' => $reprint_data,//转发的原文数据
            'is_reprint' => $this->is_reprint($model['postcontent']),//判断文章内容是否是转载 1:是,0:否
        ];

        //获取评论列表
        $res = [
            'plazaInfo' => $content_info,//内容
        ];

        PlazaPostModel::where('id', $id)->inc('view')->update();

        $this->success('', $res);
    }

    //获取论坛帖子详情
    private function getPlateShow($id)
    {
        $model = Platepost::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);//查询软删除的用户信息
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }

        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $reprint_data = [];//转发的原文信息
        if ($model['reprint_id']) {
            $reprint_article = Platepost::find($model['reprint_id']);//被转发文原文

            $r_file_img = [];
            foreach (explode(',', $reprint_article['file_img']) as $path) {
                $r_file_img[] = obsurl($path);
            }
            $r_file_video = [];
            foreach (explode(',', $reprint_article['file_video']) as $path) {
                $r_file_video[] = obsurl($path);
            }
            $r_file_audio = [];
            foreach (explode(',', $reprint_article['file_audio']) as $path) {
                $r_file_audio[] = obsurl($path);
            }
            $r_file_data = [];
            if ($reprint_article['file_data']) {
                $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                foreach ($r_attachment as $r_av) {
                    $r_file_data[] = [
                        'filename' => '复盘-' . $r_av['filename'],
                        'url' => obsurl($r_av['url'])
                    ];
                }
            }

            $reprint_data = [
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $reprint_article['id'],
                'post_title' => $reprint_article['postname'],//标题
                'post_content' => $reprint_article['postcontent'],//内容
                'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                'file_img' => array_filter($r_file_img),//图片数组
                'file_img_num' => count(array_filter($r_file_img)),//图片数组
                'file_video' => array_filter($r_file_video),//视频数组
                'file_audio' => array_filter($r_file_audio),//音频数组
                'file_data' => array_filter($r_file_data),//文件数组
            ];
        }


        $content_info = [
            'id' => $model['id'],
            'title' => $model['postname'],//帖子标题
            'content' => $model['postcontent'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['plateid'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => date('Y-m-d H:i', strtotime($model['creat_time'])),//创建时间
            'is_like' => 0,//是否点赞|0:否
            'is_favorite' => 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'reprint_id' => $model['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
            'reprint_data' => $reprint_data,//转发的原文数据
            'is_reprint' => $this->is_reprint($model['postcontent']),//判断文章内容是否是转载 1:是,0:否
        ];


        //获取评论列表
        $res = [
            'plateInfo' => $content_info,//内容
        ];

        Platepost::where('id', $id)->inc('view')->update();

        $this->success('', $res);
    }

    //获取新闻帖子详情
    private function getNewsShow($id)
    {
        $model = News::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }

        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $content_info = [
            'id' => $model['id'],
            'title' => $model['post_title'],//帖子标题
            'content' => $model['post_content'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['parent_id'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => $model['create_time'] ? date('Y-m-d H:i:s', $model['create_time']) : '',//创建时间
            'is_like' => 0,//是否点赞|0:否
            'is_favorite' => 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'is_reprint' => $this->is_reprint($model['post_content']),//判断文章内容是否是转载 1:是,0:否
        ];

        //获取评论列表
        $res = [
            'newsInfo' => $content_info,//内容
        ];

        News::where('id', $id)->inc('post_hits')->update();//浏览自增

        $this->success('', $res);
    }

    //获取视频详情
    private function getVideoShow($id)
    {
        $model = Video::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        $model = $model->toArray();
        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        if ($model['video_path']) {
            if (substr($model['video_path'], 0, 1) === '/') {
                $model['video_path'] = obsurl($model['video_path']);
            }
        }
        if ($model['image']) {
            if (substr($model['image'], 0, 1) === '/') {
                $model['image'] = obsurl($model['image']);
            }
        }
        $content_info = [
            'id' => $model['id'],
            'title' => $model['title'],//标题
            'description' => $model['description'],//描述
            'image' => $model['image'],//封面图片
            'video_path' => $model['video_path'],//视频链接
            'type' => $model['type'],//类型|1:视频,2:音频
            'user_id' => $model['user_id'],//用户id
            'is_comment' => $model['iscomment'],//是否可以评论|0:否,1:是
            'category_id' => $model['video_category_id'],//栏目ID
            'comment_count' => $model['count_comment'],//评论数量
            'post_like' => $model['count_like'],//点赞数量
            'post_favorites' => $model['count_favorite'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => (!$model['create_time'] ? '' : date('Y-m-d H:i', strtotime($model['create_time']))),//创建时间
            'is_like' => 0,//是否点赞|0:否
            'is_favorite' => 0,//是否收藏|0:否
            'average_score' => $model['average_score'],//平均分
            'user' => $model['user']
        ];

        //获取评论列表
        $res = [
            'videoInfo' => $content_info,//内容
        ];

        Video::where('id', $id)->inc('count_view')->update();//浏览自增

        $this->success('', $res);
    }

    //获取直播详情
    private function getLiveShow($id)
    {
        $model = Live::find($id);
        if (!$model) {
            $this->error('内容不存在');
        }
        $model = $model->toArray();


        if ($model['image']) {
            if (substr($model['image'], 0, 1) === '/') {
                $model['image'] = obsurl($model['image']);
            }
        }
        $content_info = [
            'id' => $model['id'],
            'category_id' => $model['live_category_id'],//栏目ID
            'title' => $model['title'],//标题
            'description' => $model['description'],//描述
            'image' => $model['image'],//封面图片
            'live_url' => $model['live_url'],//视频链接
            'live_time' => $model['live_time'],//直播时间
            'is_top' => $model['is_top'],//是否置顶|0:否,1:是
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'average_score' => $model['average_score'],//平均分
            'create_time' => $model['create_time'],//创建时间
            'rating' => 0,//评分
        ];

        //获取评论列表
        $res = [
            'liveInfo' => $content_info,//内容
        ];
        $this->success('', $res);
    }

    //获取投研日志详情
    private function getInvestmentShow($id)
    {
        $model = Investment::find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        $model = $model->toArray();

        //判断是否分类是只显示文件的
        $is_show_file = InvestmentCategoryModel::where('id', $model['investment_category_id'])->value('type');
        $file_list = [];
        if ($is_show_file == 1) {
            $file_list = json_decode($model['file_list'], true) ?? [];
        }

        $content_info = [
            'id' => $model['id'],
            'content_time' => $model['content_time'] ? date('Y-m-d H:i:s', $model['content_time']) : '',//时间
            'macro_content' => $model['macro_content'],//宏观面
            'policy_content' => $model['policy_content'],//政策面
            'fund_content' => $model['fund_content'],//资金面及预期
            'emotion_content' => $model['emotion_content'],//情绪面
            'expect_content' => $model['expect_content'],//市场预期/关注
            'news_content' => $model['news_content'],//市场新闻
            'create_time' => $model['createtime'] ? date('Y-m-d H:i:s', $model['createtime']) : '',//创建时间
            'is_show_file' => $is_show_file,//0=不展示文件,1=展示文件
            'file_list' => $file_list,
        ];

        //获取评论列表
        $res = [
            'investmentInfo' => $content_info,//内容
        ];
        $this->success('', $res);
    }

    //判断文章内容是否是转载 1:是,0:否
    private function is_reprint($content)
    {
        $prefix = '【转载--';
        $is_reprint = strpos($content, $prefix) === 0;
        return $is_reprint ? 1 : 0;
    }

    //随机获取分享封面图
    public function getRandomShareCoverImage()
    {
        $rand = rand(1,5);
        $res = [
            'cover_image_url'=>localurl("/img/shareImg/{$rand}.jpg")
        ];
        $this->success('', $res);
    }
}
